#include <stdlib.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "trace_out.h"
#include "scheduler.h"
#include <stdio.h>
#include <tr1/unordered_map>
using namespace std;
/*thread_private_info thread_info[thread_info_size];
int thread_info_id = 0;
struct thread_ctl* thread_ctl_constructor()
{
	struct thread_ctl* self = (thread_ctl*)malloc(sizeof(thread_ctl));
	self->out_filename =  NULL;
	memset(thread_info, 0, sizeof(thread_info));
	return self;
}
void thread_ctl_destruct(struct thread_ctl* self)
{
	int i;
	for(i = 0; i < thread_info_size; ++i)
    {
        fclose(thread_info[i].outfile);
    }
}
void init_self(struct thread_ctl* self, char*  mydir){
     if(mydir[0] != '\0')
     {
        strcat(mydir, "scheduler_thread_");
	    self->out_filename = mydir;
    }else 
        self->out_filename = "scheduler_thread_";
     return;
}
void end_file_spawn_self(struct thread_ctl* self)
{
    int i;
    for(i = 0; i < thread_info_id; ++i)
    {
	    fputs("spawn\n", (thread_info[i].outfile));
    }
}
FILE*  get_file_self(struct thread_ctl* self)
{
	
	unsigned int myid = get_sche_id();
	if(thread_info[myid].outfile == 0)
    {
		
        thread_private_info info;
	    char filename[20] = ""; 
        strcat(filename,self->out_filename);
        char b[5];
        sprintf(b, "%d", myid);
        char* c = ".csv";
        strcat(filename,b);
        strcat(filename,c);
		info.outfile = fopen(filename, "w+");
		thread_info[myid] = info;
		++thread_info_id;
        return info.outfile;
    }
    else
    {
        return thread_info[myid].outfile;
    }
}
void print( const char* fmt, ... )
{
    FILE*  out = get_file();

    double vargflt = 0;
    int  vargint = 0;
    char* vargpch = NULL;
    char vargch = 0;
    const char* pfmt = NULL;
    va_list vp;

    va_start(vp, fmt);
    pfmt = fmt;

    while(*pfmt)
    {
        if(*pfmt == '%')
        {
            switch(*(++pfmt))
            {
                case 'c':
                    vargch = va_arg(vp, int);
					fprintf(out, "%c", vargch); 
                    break;
                case 'd':
                case 'i':
                    vargint = va_arg(vp, int);
					fprintf(out, "%i", vargint); 
                    break;
                case 'f':
                    vargflt = va_arg(vp, double);
					fprintf(out, "%lf", vargflt); 
                    break;
                case 's':
                    vargpch = va_arg(vp, char*);
					fputs(vargpch, out);
                    break;
                case 'b':
                case 'B':
                    vargint = va_arg(vp, int);
					fprintf(out, "%d", vargint); 
                    break;
                case 'x':
                case 'X':
                    vargint = va_arg(vp, int);;
					fprintf(out, "%d", vargint); 
                    break;
                case '%':
					fputs("%", out);
                    break;
                default:
                    break;
            }
            pfmt++;
        }
        else
        {
		    fwrite(pfmt, sizeof(char),1,out);
            ++pfmt;
        }
   }
   va_end(vp);
}
void print_long_long(struct thread_ctl* self, long long data)
{
    FILE*  out = get_file_self(self);
	fprintf(out, "%lld", data); 
	fputs("\n", out);
}

thread_ctl internal_ctl ;
void init_trace_ctl(char*  mydir){
    init_self(&internal_ctl, mydir);
}

FILE*  get_file()
{
    return get_file_self(&internal_ctl);
}

void print_long_long_thread_file(long long data)
{
    print_long_long(&internal_ctl, data);
}

void print_to_internal_thread_file(const char* fmt, ... )
{
    FILE*  out = get_file_self(&internal_ctl);

    double vargflt = 0;
    int  vargint = 0;
    char* vargpch = NULL;
    char vargch = 0;
    const char* pfmt = NULL;
    va_list vp;

    va_start(vp, fmt);
    pfmt = fmt;

    while(*pfmt)
    {
        if(*pfmt == '%')
        {
            switch(*(++pfmt))
            {
                case 'c':
                    vargch = va_arg(vp, int); 
					fprintf(out, "%c", vargch); 
                    break;
                case 'd':
                case 'i':
                    vargint = va_arg(vp, int);
					fprintf(out, "%i", vargint); 
                    break;
                case 'f':
                    vargflt = va_arg(vp, double);
					fprintf(out, "%lf", vargflt); 
                    break;
                case 's':
                    vargpch = va_arg(vp, char*);
					fputs(vargpch, out);
                    break;
                case 'b':
                case 'B':
                    vargint = va_arg(vp, int);
					fprintf(out, "%d", vargint); 
                    break;
                case 'x':
                case 'X':
                    vargint = va_arg(vp, int);
					fprintf(out, "%d", vargint); 
                    break;
                case '%':
					fputs("%", out);
                    break;
                default:
                    break;
            }
            pfmt++;
        }
        else
        {
			fwrite(pfmt, sizeof(char),1,out);
            ++pfmt;
        }
   }
   va_end(vp);
}

//for user
thread_ctl th_ctl;
void print_to_thread_file(const char* fmt, ... )
{
    FILE*  out = get_file_self(&th_ctl);

    double vargflt = 0;
    int  vargint = 0;
    char* vargpch = NULL;
    char vargch = 0;
    const char* pfmt = NULL;
    va_list vp;

    va_start(vp, fmt);
    pfmt = fmt;

    while(*pfmt)
    {
        if(*pfmt == '%')
        {
            switch(*(++pfmt))
            {
                case 'c':
                    vargch = va_arg(vp, int); 
					fprintf(out, "%c", vargch);
                    break;
                case 'd':
                case 'i':
                    vargint = va_arg(vp, int);
					fprintf(out, "%i", vargint);
                    break;
                case 'f':
                    vargflt = va_arg(vp, double);
					fprintf(out, "%lf", vargflt);
                    break;
                case 's':
                    vargpch = va_arg(vp, char*);
					fputs(vargpch, out);
                    break;
                case 'b':
                case 'B':
                    vargint = va_arg(vp, int);
					fprintf(out, "%d", vargint);
                    break;
                case 'x':
                case 'X':
                    vargint = va_arg(vp, int);
					fprintf(out, "%d", vargint);
                    break;
                case '%':
					fputs("%", out);
                    break;
                default:
                    break;
            }
            pfmt++;
        }
        else
        {
		    fwrite(pfmt, sizeof(char),1,out);
            ++pfmt;
        }
   }
   va_end(vp);
}

void end_file_spawn()
{
    end_file_spawn_self(&th_ctl);
    end_file_spawn_self(&internal_ctl);
}*/
